
<?php 
include('../../Connections/KRAPPERCON.php');
$oper = '';
if (isset($_POST['oper'])){
$oper = $_POST['oper'];
}
if (isset($_GET['oper'])){
$oper = $_GET['oper'];
}

if ($oper == 'save'){
	$fecfac = getVal('t_fecfac');
	$reffac = getVal('t_reffac');
	$fecven = getVal('t_fecven');
	$feccon = getVal('t_feccon');
	$centro = getVal('cbo_centro');
	$txtfac = getVal('t_txtfac');
	$codprv = getVal('cbo_fac_prov');
	$moneda = getVal('cbo_moneda');
	$importe = getVal('t_monfac');
	$igv = getVal('t_impfac');
	$ind_imp = getVal('i_imp');
	$ind_det = getVal('i_det');
	$items = getVal('items');
	$clase = getVal('clase');
	$tipdoc = getVal('t_tipdoc');
	$monexo = getVal('t_monexo');
	$ind_per = getVal('i_per');
	$val_per = getVal('i_impper');
	$ref_per = getVal('t_refper');
	// 190000003|1|posicion1|1|2001#190000003|2|posicion2|1|2001#
	$sql = "call sp_saveMIRO('".$codprv."','".$tipdoc."','".$fecfac."','".$fecven."',".$centro.",'".$reffac."','".$txtfac."',".$importe.",".$igv.",'".$moneda."','".$items."', '".$user_now."','".$ind_imp."','".$ind_det."','".$clase."','".$monexo."','".$ind_per."','".$val_per."','".$ref_per."','".$feccon."');";
	doEcho ($sql);

	$res = $mysqli->query($sql) or die($mysqli->error.__LINE__);
//	$res = $mysqli->query($sql);
	if ($res){
	?>
	<script type="text/javascript">
	<?php 
		if ($row = $res->fetch_assoc()){
		$newnro = $row['newnro'];
			if ($newnro > 0){
			// Se grabo OK el Documento
			?>
			showOk('Se grabo la Factura Nro. <?php echo $newnro; ?> satisfactoriamente.');
			$('#sp_nofac').html('<?php echo $newnro; ?>');
			$("#tbl_itm_fac .chk").remove();
			$("#tbl_itm_fac .clsFacChk").remove();
			$('#tbl_dat_fac input,#tbl_dat_fac select').disable();
			<?php
			}
			else {
			?>
			showOk('Error al registrar la Factura : \n<?php echo $row['msg_err'].'\nDoc.Compra : '.$row['docpos']; ?>');		
			<?php
			}
		}
	?>
	</script>
	<?php 
	$res->close();
	$mysqli->check_result();
	}
}
elseif ($oper == 'anularFC'){
//CALL SP_updNumerador('FACTCOM',NEWNRO);
$factura = $_POST['factura'];
$motivo = $_POST['motivo'];
$sql = "call sp_anularMIRO('".$factura."','".$user_now."','".$motivo."')";
doEcho($sql);
$res = $mysqli->query($sql);
	if ($res){
		if($row = $res->fetch_assoc()){
			if (trim($row['newnro'])>0){
			echo "<script type='text/javascript'>showOk('Se ha anulado el Documento Correctamente. Anulacion :".$row['newnro']."');
			GEBI('sp_nofac').innerHTML='".$row['newnro']."';</script>";			
			}
			else {
			echo "<script type='text/javascript'>showError('".$row['msg_err']."');</script>";
			}
		}
	$res->close();
	
	$mysqli->check_result();
	
	}

}
elseif ($oper == 'folio'){
$factura = $_GET['factura'];
$sql = "call sp_doFactura('".$factura."','".$user_now."')";
doEcho($sql);
$res = $mysqli->query($sql);
	if ($res){
		if($row = $res->fetch_assoc()){
			if (trim($row['w_new_folio'])<>''){
			echo "<script type='text/javascript'>showOk('Se ha generado el Documento con referencia : ".$row['w_new_folio']."'); GEBI('t_fc_nrofol').value='".$row['w_new_folio']."';$('#btn_vf_prnt').css('display','');</script>";			
			}
			else {
			echo "<script type='text/javascript'>showError('".$row['msg_err']."');</script>";
			}
		}
	$res->close();
	$mysqli->check_result();
	}
}
elseif ($oper == 'anular'){
$factura = $_POST['factura'];
$sql = "call sp_deleteVBRK('".$factura."','".$user_now."',0)";
doEcho($sql);
$res = $mysqli->query($sql);
	if ($res){
		if($row = $res->fetch_assoc()){
			if (trim($row['newdoc'])>0){
			echo "<script type='text/javascript'>showOk('Se ha anulado el Documento Correctamente. Anulacion :".$row['newdoc']."');</script>";			
			}
			else {
			echo "<script type='text/javascript'>showError('".$row['msg_err']."');</script>";
			}
		}
	$res->close();
	$mysqli->check_result();
	}
}
elseif ($oper == 'comp' ){
$factura = $_POST['factura'];
$sql = "call sp_anularFact('".$factura."','".$user_now."',1)";
doEcho($sql);
$res = $mysqli->query($sql);
	if ($res){
		if($row = $res->fetch_assoc()){
			if (trim($row['newdoc'])>0){
			echo "<script type='text/javascript'>showOk('Se ha generado el Doc.Compensacion ".$row['newdoc']."');</script>";			
			}
			else {
			echo "<script type='text/javascript'>showError('".$row['msg_err']."');</script>";
			}
		}
	$res->close();
	$mysqli->check_result();
	}
}
elseif ($oper == 'pago'){
	$factura = getVal('factura');
	$sql = "call sp_pagarFactComp('".$factura."','".$user_now."')";
	doEcho($sql);
	$res = $mysqli->query($sql);
	if ($res){
		if($row = $res->fetch_assoc()){
			if ($row['newnro'] > 0) {
				echo "<script type='text/javascript'>showOk('Se ha registrado el Pago correctamente para el Documento : ".$factura."')</script>";
			}
			else {
				echo '<script type="text/javascript">showError("'.$row['msg_err'].'");</script>';
			}
		}
	$res->close();
	$mysqli->check_result();
	}
}
elseif ($oper == 'guia'){
	$factura = getVal('factura');
	$destino = getVal('Destino');
	$partida = getVal('Partida');
	
	$idcli = trim(getVal('ClientID'));
	if ($idcli == '[Auto]') $idcli = 0;
	$nfcli = getVal('ClientNIF');
	$nocli = getVal('ClientNom');
	$dircli = getVal('ClientDir');
	$texto = getVal('texto');
	
	$sql = "call sp_doGuiaFac('".$factura."','".$user_now."','".$idcli."','".$nfcli."','".$nocli."','".$dircli."','".$texto."','".$partida."','".$destino."')";
	doEcho($sql);
	$res = $mysqli->query($sql);
	if ($res){
		if($row = $res->fetch_assoc()){
			$guia = trim($row['guia']);
			if ($guia != '') {
				echo "<script type='text/javascript'>closePopup();showOk('Se ha generado correctamente la Guia : ".$guia."');$('#btn_do_guia').val('".$guia."').attr('title','".$guia."');</script>";
			}
			else {
				echo '<script type="text/javascript">showError("'.$row['msg_err'].'");</script>';
			}
		}
	$res->close();
	$mysqli->check_result();
	}
}
elseif ($oper == 'busNIF') {
	$nif = getVal('nif');
	$sql = "select ClientNom,ClientDirec,ClientID,ClientNIF from tt_cliente where ClientNIF='".$nif."'";
	doEcho($sql);	
	$res = $mysqli->query($sql);
	if ($res){
		if ($mysqli->affected_rows == 1) {
		$row=$res->fetch_assoc();
		?>
        <script type="text/javascript">
		GEBI('t_gu_cdc').value = '<?php echo $row['ClientID']; ?>';
		GEBI('t_gu_raz').value = '<?php echo $row['ClientNom']; ?>';
		GEBI('t_gu_dir').value = '<?php echo $row['ClientDirec']; ?>';
		//GEBI('t_gu_nif3').value = '<?php echo $row['ClientNIF']; ?>';
        </script>
        <?php  
		}
		else {
?>
        <table width="100%" border="1" cellspacing="0" cellpadding="0" class="tableIn" rules="all" style="margin-bottom:10px;">
        <thead>
          <tr>
            <th>Codigo</th>
            <th>RUC/DNI</th>
            <th>Cliente</th>
            <th>Direccion</th>
          </tr>
          </thead>
          <tbody>
          <?php 
		  while($row = $res->fetch_assoc()){
		  ?>
          <tr>
            <td><a class="a" href="#t_gu_nif" onclick="selCliNif(this,<?php echo getVal('resp');?>)"><?php echo $row['ClientID']; ?></a></td>
            <td class="b"><?php echo $row['ClientNIF']; ?></td>
            <td class="c"><?php echo $row['ClientNom']; ?></td>
            <td class="d"><?php echo $row['ClientDirec']; ?></td>
          </tr>
          <?php 
		  }
		  ?>
		</tbody>	
        </table>
<?php 		
		}
	$res->close();
	$mysqli->check_result();
	}
}
?>